<template>
  <el-card class="mt-10">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!cartgroyStroe.cartgroy3Id"
      @click="showSpuList"
    >
      添加SPU
    </el-button>
    <el-table border :data="spuList" class="mt-20">
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <el-tooltip effect="light" content="添加SKU" placement="bottom">
            <el-button
              type="primary"
              :icon="Plus"
              size="small"
              @click="showAddSku(row)"
            />
          </el-tooltip>
          <el-tooltip effect="light" content="修改SPU" placement="bottom">
            <el-button
              type="warning"
              :icon="Edit"
              size="small"
              @click="updateSpuAttr(row)"
            />
          </el-tooltip>
          <el-tooltip effect="light" content="查看SKU列表" placement="bottom">
            <el-button type="info" :icon="InfoFilled" size="small" />
          </el-tooltip>
          <el-tooltip effect="light" content="删除SPU" placement="bottom">
            <el-button
              type="danger"
              :icon="Delete"
              size="small"
              @click="deleteSpuAttr(row, $index)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      :background="background"
      layout="prev, pager, next, jumper, ->, total, sizes"
      :total="total"
      @size-change="getSpuList"
      @current-change="getSpuList"
      class="mt-20"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script setup lang="ts">
import { ref, watch } from "vue";
import { Plus, Edit, InfoFilled, Delete } from "@element-plus/icons-vue";
import { useCartgroyStroe } from "@/stores/cartgroy";
import { getSpuListApi, deleteSpuAttrApi } from "@/api/product/spu";
import type { SpuList, SpuItem } from "@/api/product/model/spuModel";
import { useSpuStore } from "@/stores/spu";
import { ElMessage } from "element-plus";

const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const background = ref(false);

const spuList = ref<SpuList>([]);

const cartgroyStroe = useCartgroyStroe();

const emit = defineEmits(["isShow"]);

const spuStore = useSpuStore();

// 修改SPU
const updateSpuAttr = (row: SpuItem) => {
  const { id, spuName, description, tmId } = row;
  spuStore.$patch({
    id,
    spuName,
    description,
    tmId,
  });
  emit("isShow", 2);
};

// 显示添加或修改spu
const showSpuList = () => {
  spuStore.$reset();
  emit("isShow", 2);
};
// 显示添加SKU
const showAddSku = (row: SpuItem) => {
  const { id, spuName } = row;
  spuStore.$patch({
    id,
    spuName,
  });
  emit("isShow", 3);
};

// 删除SPU
const deleteSpuAttr = async (row: SpuItem, index: number) => {
  await deleteSpuAttrApi(row.id);
  // spuList.value.splice(index, 1);
  ElMessage.success("删除属性成功");
  getSpuList();
};

const getSpuList = async () => {
  const result = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: cartgroyStroe.cartgroy3Id as number,
  });
  spuList.value = result.records;
  total.value = result.total;
};

watch(
  () => cartgroyStroe.cartgroy3Id,
  (newVal) => {
    if (!newVal) {
      currentPage.value = 1;
      total.value = 0;
      spuList.value = [];
      return;
    }
    getSpuList();
  },
  {
    immediate: true,
  }
);
</script>

<style scoped></style>
